package com.huika.renrench.control.my.adapter;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huika.lib.utils.MoneyShowTool;
import com.huika.renrench.R;
import com.huika.renrench.control.common.base.BaseAda;
import com.huika.renrench.control.my.activity.BorrowRecordActivity;
import com.huika.renrench.control.my.activity.MoneyDetailsActivity;
import com.huika.renrench.support.bean.BorrowRecordBean;
import com.huika.renrench.utils.DateTimeTool;

/**
 * @author yangyuan
 * @date：2015年6月3日
 * @describe:借款管理	
 */
public class BorrowRecordAdapter extends BaseAda<BorrowRecordBean>  {
	
	private BorrowRecordActivity context;
	private int type;

	public BorrowRecordAdapter(BorrowRecordActivity context,int type) {
		super(context);
		this.context = context;
		this.type = type;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_borrow_record, parent, false);
			vh = new ViewHolder(convertView);
			convertView.setTag(vh);
		}else{
			vh = (ViewHolder) convertView.getTag();
		}
		BorrowRecordBean bean = group.get(position);
		vh.text_time.setText(DateTimeTool.ymdhmsDate(bean.getCreateTime()));
		vh.text_money.setText(MoneyShowTool.twolastDFStr(bean.getTargetAmount()) + "元");
		SetViewState(vh.text_state,bean.getLoanState());
		vh.lin_init.setOnClickListener(new MyClick(bean));
		return convertView;
	}
	
	class ViewHolder{
		public TextView text_time;
		public TextView text_money;
		public TextView text_state;
		private LinearLayout lin_init;
		
		public ViewHolder(View convertView) {
			text_time = (TextView) convertView.findViewById(R.id.text_time);
			text_money = (TextView) convertView.findViewById(R.id.text_money);
			text_state = (TextView) convertView.findViewById(R.id.text_state);
			lin_init = (LinearLayout) convertView.findViewById(R.id.lin_init);
	}
}

class MyClick implements OnClickListener{
	
	BorrowRecordBean bean;
	
	public MyClick(BorrowRecordBean bean) {
		super();
		this.bean = bean;
	}

	@Override
	public void onClick(View v) {
		Bundle bundle = new Bundle();
		bundle.putInt("type", type);
		bundle.putSerializable("BorrowRecordBean", bean);
		context.showActivity(context, MoneyDetailsActivity.class,bundle);
	}
   }

	private void SetViewState(TextView text_state,int LoanState){
		if (type==0) {
			if (LoanState==0) {
				text_state.setText("借款中");
				text_state.setTextColor(context.getResources().getColor(R.color.color_58bdee));
			}else if (LoanState==1)  {
				text_state.setText("已还款");
				text_state.setTextColor(context.getResources().getColor(R.color.color_5eb62f));
			}else {
				text_state.setText("");
			}
		}else {
			if (LoanState==0) {
				text_state.setText("审核中");
				text_state.setTextColor(context.getResources().getColor(R.color.color_fd6138));
			}else if (LoanState==1)  {
				text_state.setText("已通过");
				text_state.setTextColor(context.getResources().getColor(R.color.color_5eb62f));
			}else if (LoanState==2)  {
				text_state.setText("已拒绝");
				text_state.setTextColor(context.getResources().getColor(R.color.color_969696));
			}else {
				text_state.setText("");
			}
		}
	}

}
